//
//  TodayVideoViewController.m
//  SYiDaiy
//
//  Created by dllo on 16/9/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TodayVideoViewController.h"
#import "Masonry.h"
#import "TAMacro.h"
#import <AVFoundation/AVFoundation.h>


@class TodayViewController;
@interface TodayVideoViewController ()
@property (nonatomic,strong) UISlider *slider;
@property (nonatomic,strong) UIButton *playButton;
@property (nonatomic,strong) UIButton *nextButton;
@property (nonatomic,strong) UIButton *lastButton;
@property (nonatomic,strong) UISlider *volumeSlider;
@property (nonatomic,strong) UIButton *finishButton;
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UIView *background;
@property (nonatomic,strong) UILabel *timeLabel;
@property (nonatomic,strong) UILabel *totalTimeLabel;
@property (nonatomic,strong) UIView *alphaView;

@property (nonatomic,strong) AVPlayer *player;
@property (nonatomic,strong) AVPlayerItem *playerItem;
@property (nonatomic,strong) AVPlayerLayer *playerLayer;

@property (nonatomic,assign) CGFloat totalTime;
@end

@implementation TodayVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    [self createVideoView];
}

- (void)createVideoView
{
    _alphaView = [UIView new];
    _alphaView.frame = self.view.bounds;
    _alphaView.backgroundColor = [UIColor blackColor];
    _alphaView.alpha = 0.2;
    [self.view addSubview:_alphaView];
    
    
    _finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    _finishButton.backgroundColor = [UIColor redColor];
    [_finishButton setTitle:@"完成" forState:UIControlStateNormal];
    [_finishButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_finishButton addTarget:self action:@selector(G:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_finishButton];
    [_finishButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(5);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(50);
    }];
    _timeLabel = [UILabel new];
    _timeLabel.text = @"0:00";
    _timeLabel.textColor = [UIColor blackColor];
    [self.view addSubview:_timeLabel];
    [_timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_finishButton.mas_right).mas_equalTo(5);
        make.top.mas_equalTo(20);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(30);
    }];
    
    
    _totalTimeLabel = [UILabel new];
    [self.view addSubview:_totalTimeLabel];
    _totalTimeLabel.text = @"0:30";
    
    
    [_totalTimeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.right.mas_equalTo(10);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(30);
    }];
    _slider = [[UISlider alloc] init];
    [_slider addTarget:self action:@selector(sliderClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_slider];
    [_slider mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(_timeLabel.mas_right).mas_equalTo(5);
        make.height.mas_equalTo(30);
        make.right.mas_equalTo(_totalTimeLabel.mas_left).mas_equalTo(-5);
    }];
    
    _backView = [UIView new];
    _backView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_backView];
    [_backView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_finishButton.mas_bottom).mas_equalTo(5);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(SCREEN_HEIGHT - 200);
    }];
    //
    _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_playButton setImage:[UIImage imageNamed:@"player_btn_pause_normal@2x副本"] forState:UIControlStateNormal];
    [_playButton addTarget:self action:@selector(playButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_playButton];
    [_playButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_backView.mas_bottom).mas_equalTo(5);
        make.centerX.mas_equalTo(0);
        make.width.height.mas_equalTo(50);
    }];
    
    _lastButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_lastButton setImage:[UIImage imageNamed:@"fllplayer_btn_pre_normal"] forState:UIControlStateNormal];
    [_lastButton addTarget:self action:@selector(lastButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_lastButton];
    [_lastButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_backView.mas_bottom).mas_equalTo(5);
        make.left.mas_equalTo(SCREEN_WIDTH / 4 - 25);
        make.width.height.mas_equalTo(50);
    }];
    //
    _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_nextButton setImage:[UIImage imageNamed:@"player_btn_next_normal-1"] forState:UIControlStateNormal];
    [_nextButton addTarget:self action:@selector(nextButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_nextButton];
    [_nextButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_backView.mas_bottom).mas_equalTo(5);
        make.right.mas_equalTo(-(SCREEN_WIDTH / 4 - 25));
        make.width.height.mas_equalTo(50);
    }];
    
    
    
    _volumeSlider = [[UISlider alloc] init];
    [_volumeSlider addTarget:self action:@selector(volumeSliderClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_volumeSlider];
    [_volumeSlider mas_updateConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(20);
        make.top.mas_equalTo(_playButton.mas_bottom).mas_equalTo(10);
        
    }];
    
    
    
    [self addPlayer:_str];
    
    _player.volume = 1;
    
    _volumeSlider.value = 5.0;
    _volumeSlider.minimumValue = 0.0;
    _volumeSlider.maximumValue = 10.0;
}


- (void)addPlayer:(NSString *)str
{

    

//
    NSURL *url = [NSURL URLWithString:str];

    self.playerItem = [[AVPlayerItem alloc]initWithURL:url];

    self.player = [AVPlayer playerWithPlayerItem:self.playerItem];

    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];

    self.playerLayer.frame = CGRectMake(0, 160, SCREEN_WIDTH, 200);

    self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;


    [self.backView.layer insertSublayer:self.playerLayer atIndex:0];

    
    

    
    
    

}
//按钮点击
- (void)playButtonClicked
{

    [self.player play];
    [_playButton setImage:[UIImage imageNamed:@"player_btn_pause_normal@2x副本"] forState:UIControlStateNormal];
    [_playButton addTarget:self action:@selector(pauseButton) forControlEvents:UIControlEventTouchUpInside];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sliderChange) userInfo:nil repeats:YES];


}

- (void)G:(UIButton *)finishButton
{
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    
    [self.navigationController popViewControllerAnimated:YES];

    
}

- (void)sliderChange
{
    _slider.value = CMTimeGetSeconds(_player.currentItem.currentTime) / (CMTimeGetSeconds(_player.currentItem.duration));
   NSInteger current =  _player.currentTime.value / _player.currentTime.timescale;
    NSInteger total = _player.currentItem.duration.value / _player.currentItem.duration.timescale;
    if (current < 10) {
        _timeLabel.text = [NSString stringWithFormat:@"0:0%ld",current];
    } else {
        _timeLabel.text = [NSString stringWithFormat:@"0:%ld",current];
    }

    NSInteger minite = total / 60;
    NSInteger second = total % 60;
    _totalTimeLabel.text = [NSString stringWithFormat:@"%ld:%ld",minite,second];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
    if (current == total) {
        self.navigationController.navigationBar.hidden = NO;
        self.tabBarController.tabBar.hidden = NO;
        [self.navigationController popViewControllerAnimated:YES];
    }
    });
    
    
}

- (void)sliderClicked
{

    CMTimeValue totalTime =  _player.currentItem.duration.value;
    CMTimeScale scale =  _player.currentItem.duration.timescale;
    _totalTime = totalTime / scale;

    CMTime time =  CMTimeMakeWithSeconds(_totalTime * _slider.value, _player.currentItem.duration.timescale);
    [_player seekToTime:time completionHandler:^(BOOL finished) {
        [_player play];
    }];
    
}

- (void)volumeSliderClicked
{
    _player.volume = _volumeSlider.value;

    
    
    
}

- (void)pauseButton
{
    [_player pause];
    [_playButton setImage:[UIImage imageNamed:@"player_btn_play_normal-1"] forState:UIControlStateNormal];
    [_playButton addTarget:self action:@selector(playButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)lastButtonClicked
{
    _slider.value = (CMTimeGetSeconds(_player.currentItem.currentTime) - 1) / (CMTimeGetSeconds(_player.currentItem.duration));
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [_player pause];
//    });
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [_player play];
//    });
}

- (void)nextButtonClicked
{
    _slider.value = (CMTimeGetSeconds(_player.currentItem.currentTime) + 1) / (CMTimeGetSeconds(_player.currentItem.duration));
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
